<?php
namespace app\common\business;

use app\common\model\mysql\Specs as SpecsModel;

class Specs
{

    public $model = null;

    public function __construct()
    {
        $this->model = new SpecsModel();
    }

    public function add($data)
    {
        if (empty($data) || !is_array($data)) {
            throw new \think\Exception('数据异常');
        }

        $data['status'] = SpecsModel::STATUS_NORMAL;

        $name = $data['name'];
        $res = $this->model->findSpecsByname($name);
        if ($res) {
            throw new \think\Exception('规格名已存在, 请重新添加');
        }

        try {
            $this->model->save($data);
        } catch (\Exception $e) {
            throw new \think\Exception('数据库内部异常');
        }
        return $this->model->id;
    }

    public function updateCategoryName($data)
    {
        if (empty($data) || !is_array($data)) {
            throw new \think\Exception('数据异常');
        }

        $data['status'] = CategoryModel::STATUS_NORMAL;

        $res = $this->getById($data['id']);
        if (!$res) {
            throw new \think\Exception('不存在该记录');
        }

        $name = $data['name'];
        $res = $this->model->findCategoryByname($name);
        if ($res) {
            throw new \think\Exception('分类名已存在, 请重新添加');
        }

        $updateData = [
            'name' => $data['name'],
        ];
        try {
            $res =  $this->model->updateById($data['id'],$updateData);
        } catch (\Exception $e) {
            throw new \think\Exception('数据库内部异常');
        }
        return $res;
    }

    public function getNormalCategorys(): array
    {
        $field = "id, name, pid";
        $categorys = $this->model->getNormalCategorys($field);
        if (!$categorys) {
            return [];
        }

        return $categorys->toArray();
    }

    public function getLists( $num): array
    {
        $list = $this->model->getLists( $num);
        if (!$list) {
            return [];
        }

        $result = $list->toArray();

        $result['render'] = $list->render();

        return $result;
    }


    public function getById($id): array
    {
        $result =  $this->model->find($id);
        if (!$result) {
            return [];
        }
        return $result->toArray();
    }


    public function listorder($data)
    {
        if(!isset($data['id']) || !isset($data['listorder'])) {
            return false;
        }

        $res = $this->getById($data['id']);
        if (!$res) {
            throw new \think\Exception('不存在该记录');
        }

        $updateData = [
            'listorder' => intval($data['listorder']),
        ];

        try {
            $res = $this->model->updateById($data['id'], $updateData);
        } catch (\Exception $e) {
            return false;
        }
        return $res;
    }


    public function status($data)
    {
        if(!isset($data['id']) || !isset($data['status'])) {
            return false;
        }

        $res = $this->getById($data['id']);
        if (!$res) {
            throw new \think\Exception('不存在该记录');
        }

        if ($res['status'] == $data['status']) {
            throw new \think\Exception('两次状态修改相同');
        }

        $updateData = [
            'status' => intval($data['status']),
        ];

        try {
            $res = $this->model->updateById($data['id'], $updateData);
        } catch (\Exception $e) {
            return false;
        }
        return $res;
    }

    public function getNormalByPid($pid = 0, $field = 'id, name, pid')
    {
        try {
            $res = $this->model->getNormalByPid($pid, $field);
        } catch (\Exception $e) {
            return [];
        }
        return $res->toArray();
    }
}